package com.xiangyi.teachingsystem.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.api.R;
import com.xiangyi.teachingsystem.entity.dto.GroupDto;
import com.xiangyi.teachingsystem.entity.dto.UserGroupTaskCourseDto;
import com.xiangyi.teachingsystem.entity.po.GroupUser;
import com.xiangyi.teachingsystem.entity.po.Task;
import com.xiangyi.teachingsystem.entity.po.User;
import com.xiangyi.teachingsystem.entity.po.UserGroup;
import com.xiangyi.teachingsystem.entity.vo.Result;
import com.xiangyi.teachingsystem.service.GroupUserService;
import com.xiangyi.teachingsystem.service.TaskService;
import com.xiangyi.teachingsystem.service.UserGroupService;
import com.xiangyi.teachingsystem.service.UserService;
import com.xiangyi.teachingsystem.validation.groups.TaskInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;

@RestController
public class UserGroupController {
    @Autowired
    UserGroupService userGroupService;
    @Autowired
    TaskService taskService;
    @Autowired
    UserService userService;
    @Autowired
    GroupUserService groupUserService;

    @PostMapping("/addGroup")
    public Result addGroup(@Validated @RequestBody UserGroup userGroup){
        Task taskInfo = taskService.getById(userGroup.getTaskId());
        if (taskInfo==null)
            return Result.failure("任务不存在，请检查taskId");
        User userInfo = userService.getById(userGroup.getLeaderId());
        if (userInfo==null)
            return Result.failure("用户不存在,请检查leaderId");
        UserGroup groupInfo = userGroupService.getOne(new QueryWrapper<UserGroup>()
                .eq("group_name", userGroup.getGroupName())
                .eq("task_id",userGroup.getTaskId()));
        if (groupInfo!=null)
            return Result.failure("组名已存在");
        try {
            userGroupService.saveGroupAndLeader(userGroup);
            return Result.success("小组新建成功");
        }catch (Exception e){
            e.printStackTrace();
            return Result.failure("服务器错误").setData(e.getMessage());
        }
    }

    @PostMapping("/updateGroup")
    public Result updateGroup(@Validated(TaskInfo.class) @RequestBody UserGroup userGroup){
        UserGroup groupInfo = userGroupService.getById(userGroup.getId());
        if (!Objects.equals(groupInfo.getGroupName(), userGroup.getGroupName())){
            UserGroup group_name = userGroupService.getOne(new QueryWrapper<UserGroup>().eq("group_name", userGroup.getGroupName()));
            if (group_name!=null)
                return Result.failure("小组名称已存在");
        }

        try {
            userGroupService.updateById(userGroup);
            return Result.success("小组信息更新成功");
        }catch (Exception e){
            e.printStackTrace();
            return Result.failure("服务器错误").setData(e.getMessage());
        }
    }

    @GetMapping("/groupList")
    public Result groupList(@RequestParam Integer taskId){
        return Result.success("小组信息查询成功").setData(userGroupService.groupList(taskId));
    }

    @GetMapping("/delGroup")
    public Result delGroup(@RequestParam Integer groupId){
        try {
            userGroupService.delGroup(groupId);
            return Result.success("小组删除成功");
        }catch (Exception e){
            e.printStackTrace();
            return Result.failure("服务器错误").setData(e.getMessage());
        }
    }

    @GetMapping("/getGroupsByUserId")
    public Result getGroupsByUserId(@RequestParam Integer userId){
        try {
            List<UserGroupTaskCourseDto> groups= userGroupService.getGroupsByUserId(userId);
            return Result.success("加入的小组信息列表查询成功").setData(groups);
        }catch (Exception e){
            e.printStackTrace();
            return Result.failure("服务器错误").setData(e.getMessage());
        }
    }

    @GetMapping("/group/getById")
    public Result getById(@RequestParam Integer groupId){

        return Result.success("小组信息查询成功").setData(userGroupService.getInfoById(groupId));
    }

    @GetMapping("/listByUserAndTaskIds")
    public Result listByUserAndTaskIds(@RequestParam Integer userId,
                                       @RequestParam Integer taskId){
        List<GroupDto> groupDtoList=userGroupService.listByUserAndTaskIds(userId,taskId);
        return Result.success("所在小组列表查询成功").setData(groupDtoList);
    }
}

